<?php 
/*
 * 接口：/flow_approval/add
 * 说明：新增审核流程
 */


//获取请求参数
$params = $route->bodyParams;

//查询在该审核流中根节点是否已经存在
$searchParams = array(
  "ofa_offid"     => $params['ofa_offid'],
  "ofa_root_osid" => $params['ofa_root_osid'],
  "ofa_is_delete" => 0
);

$rsData = $db->mysqlDB->params($searchParams)->select('easyd_oa_flow_approval');


if(count($rsData['data'])>0){
  $response->responseData(false,"抱歉，该根节点在该流程中已存在，根节点不可重复");
}


//关闭事物自动提交
$db->mysqlDB->falseAutoCommit();

//新增审核流
$P1 = array(
  "ofa_offid" => $params['ofa_offid'],
  "ofa_root_osid" => $params['ofa_root_osid'],
  "ofa_son_osid" => $params['ofa_son_osid']
);

$R1 = $db->mysqlDB->automaticAdd($P1)->insert("easyd_oa_flow_approval");

//添加审核流程节点与用户

//移除根节点数据信息
array_shift($params['flowData']);

//新增是否有出错的记录
$isAddError = false;

for($i=0;$i<count($params['flowData']);$i++){

  for($k=0;$k<count($params['flowData'][$i]['node_user']);$k++){

    $P2 = array(
      "ofam_osid"  => $params['flowData'][$i]['node_id'],
      "ofam_uid"   => $params['flowData'][$i]['node_user'][$k],
      "ofam_ofid"  => $params['ofa_offid'],
      "ofam_ofaid" => $R1
    );

    $R2 = $db->mysqlDB->automaticAdd($P2)->insert("easyd_oa_flow_approval_middle");

    //如果有出错则进行记录
    if(!$R2){
      $isAddError = true;
      break;
    }

  }

}

//判断是否有出错
if(!$R1 || $isAddError){
  $db->mysqlDB->rollBackCommint();
  $db->mysqlDB->trueAutoCommit();
  $response->responseData(false);
}else{
  $db->mysqlDB->actionCommint();
  $db->mysqlDB->trueAutoCommit();
  $response->responseData(true);
}

?>